#include "FtServer.hpp"

FtServer::FtServer(int port, const std::string &storage, boost::asio::io_service &ios) : storage(storage), acceptor(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
  std::cout << "Server started on port " << port << "." << std::endl;
  std::cout << "storage path: " << storage << std::endl;
}

void				FtServer::start()
{
  TcpConnection::pointer	new_connection = TcpConnection::create(acceptor.get_io_service());

  acceptor.async_accept(new_connection->socket(),
			boost::bind(&FtServer::handleAccept,
				    this,
				    new_connection,
				    boost::asio::placeholders::error));
}

void				FtServer::handleAccept(TcpConnection::pointer new_connection, const boost::system::error_code& error)
{
  if (!error)
    new_connection->start(storage);
  std::cout << "new accept" << std::endl;
  start();
}
